package es.uc3m.setichat.android.activity;

import android.app.Activity;
import android.app.ListFragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import es.uc3m.setichat.android.SeTIChatService;


/**
 * Activity that list a number of titles of all the conversations taken with the user. 
 *  
 * @author Guillermo Suarez de Tangil <guillermo.suarez.tangil@uc3m.es>
 * @author Jorge Blasco Alis <jbalis@inf.uc3m.es> 
 */

public class SeTIChatConversationsActivity  extends Activity {
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.i("ChatsLayout", "Activity created");
        
        //setContentView(R.layout.chats_layout);
    }
    

    /**
     * This is the "top-level" fragment, showing a list of items that the
     * user can pick.  Upon picking an item, it takes care of displaying the
     * data to the user as appropriate based on the current UI layout.
     */
    public static class SeTIChatTitlesFragment extends ListFragment {
        boolean mDualPane;
        int mCurCheckPosition = 0;

        private boolean DEBUG = false;
    	private SeTIChatService mBoundService;

    	private ServiceConnection mConnection = new ServiceConnection() {
    		
    		public void onServiceConnected(ComponentName className, IBinder service) {
    	        // This is called when the connection with the service has been
    	        // established, giving us the service object we can use to
    	        // interact with the service.  Because we have bound to a explicit
    	        // service that we know is running in our own process, we can
    	        // cast its IBinder to a concrete class and directly access it.
    	        mBoundService = ((SeTIChatService.LocalBinder)service).getService();
    	        
    	        DEBUG = mBoundService.getDebug();
    	            	        
                render();
                
    	    }

    	    public void onServiceDisconnected(ComponentName className) {
    	        // This is called when the connection with the service has been
    	        // unexpectedly disconnected -- that is, its process crashed.
    	        // Because it is running in our same process, we should never
    	        // see this happen.
    	    	
    	    	if(DEBUG)
    	        	Log.d("TitlesFragment", "onServiceDisconnected: un-bounding the service");
    	    }
    	};

        
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            
            if(mBoundService == null){
	            //Binding the activity to the service to get shared objects
	            if(DEBUG)
	            	Log.d("SeTIChatConversationActivity", "Creating activity");
	            getActivity().bindService(new Intent(getActivity(), SeTIChatService.class), mConnection, Context.BIND_AUTO_CREATE);
            }else{
            	
                render();
            }
            
        }
        
        @Override
		public void onStop(){
        	super.onStop();
           	if(mBoundService != null){
        		mBoundService.stopSelf();

            	if(DEBUG)
                	Log.d("SeTIChatConversationActivity", "Unbinding activity");
        	}
        }

        private void render() {
        	//Populate conversations...
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_activated_1, 
                    new String[]{"Last Conversation", "Previous one", "Next one", "...", "First of all of them"})); //Ey, a more fancy layout could be used! Be brave!
		}

		@Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putInt("curChoice", mCurCheckPosition);
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
        	
            Intent intent = new Intent();
            intent.setClass(getActivity(), SeTIChatConversationActivity.class);
            intent.putExtra("index", position);
            startActivity(intent);
        }
    }
        
}